# -*- coding: utf-8 -*- 
# 作者:飞虎
# 开发时间:2022/5/18 22:09
from PyQt5.Qt import *


class Window(QWidget):
	def __init__(self):
		super().__init__()
		self.setWindowTitle('QFormLayout学习')
		self.resize(500, 500)
		self.setup_ui()

	def setup_ui(self):

		# name_label = QLabel('姓名(&n):')
		age_label = QLabel('年龄(&g):')
		#
		name_le = QLineEdit()
		age_sb = QSpinBox()

		# name_label.setBuddy(name_le)
		# age_label.setBuddy(age_sb)


		sex_label = QLabel('性别:')
		male_rb = QRadioButton('男')
		female_rb = QRadioButton('女')
		h_layout = QHBoxLayout()
		h_layout.addWidget(male_rb)
		h_layout.addWidget(female_rb)
		#
		#
		submit_btn = QPushButton('提交')

		# 1. 创建布局管理器

		layout=QFormLayout()
		# 2.把布局管理器赋值给需要布局的父控件
		self.setLayout(layout)
		# 3.把需要布局的子控件交给布局管理器进行布局
		# layout.addWidget(name_label)
		# layout.addWidget(name_le)
		layout.addRow("姓名(&n):",name_le)
		# layout.addRow(name_label,name_le)
		layout.addRow(sex_label,h_layout)
		layout.addRow(age_label,age_sb)
		# layout.addRow("年龄(&g)",age_sb)
		layout.addRow(submit_btn)
		# layout.insertRow(1,"性别:",h_layout)

		print(layout.labelForField(name_le).setText('xxx'*10))

		print(layout.formAlignment() == Qt.AlignLeft | Qt.AlignTop)
		# layout.setFormAlignment(Qt.AlignLeft | Qt.AlignBottom)
		# layout.setFormAlignment(Qt.AlignLeft | Qt.AlignVCenter)
		layout.setLabelAlignment(Qt.AlignRight)

		layout.setVerticalSpacing(60)
		layout.setHorizontalSpacing(20)
		layout.setFieldGrowthPolicy(QFormLayout.FieldsStayAtSizeHint)

		# layout.setRowWrapPolicy(QFormLayout.WrapLongRows)
		# layout.setRowWrapPolicy(QFormLayout.WrapAllRows)
		# layout.setFormAlignment()

		# print(layout.rowCount())
		# print(layout.getWidgetPosition(name_label))
		# print(layout.getLayoutPosition(h_layout))
		#
		# layout.setWidget(0,QFormLayout.LabelRole,name_label)
		# layout.setWidget(0,QFormLayout.FieldRole,name_le)
		#
		# layout.setLayout(1,QFormLayout.FieldRole,h_layout)
		# layout.takeRow()

		# age_sb.destroyed.connect(lambda :print('xxx1'))
		# age_label.destroyed.connect(lambda :print('xxx2'))
		# layout.removeRow(2)
		# print(layout.takeRow(2).labelItem.widget())
		# print(layout.takeRow(2).fieldItem)
		# age_sb.hide()
		# age_label.hide()
		# print(dir(QFormLayout.TakeRowResult))
		# layout.removeRow(h_layout)
		# layout.removeWidget(age_label)
		# age_label.setParent(None)

		# layout.labelForField()


if __name__ == '__main__':
	import sys

	app = QApplication(sys.argv)
	window = Window()
	window.show()
	sys.exit(app.exec_())